package com.jec.utils;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import lombok.extern.log4j.Log4j;

@Log4j
public class Directory {

	private String path = null;

	public static Directory byDirectly(String path) {
		return new Directory(path);
	}

	public static Directory byUserHome(String name) {
		// make sure the store path is exist
		String dir = System.getProperty("user.home");
		dir += File.separator + name;
		File f = new File(dir);
		if(!f.exists()) {
			if(!f.mkdirs())
				log.warn("Create store path failed. -> " + dir);
		}
		return new Directory(dir);
	}

	public static Directory byUserDir(String name) {
		// make sure the store path is exist
		String dir = System.getProperty("user.dir");
		dir += File.separator + name;
		File f = new File(dir);
		if(!f.exists()) {
			if(!f.mkdirs())
				log.warn("Create store path failed. -> " + dir);
		}
		return new Directory(dir);
	}

	private Directory(String path) {
		this.path = path;
	}

	public String getPath() {
		return path;
	}

	public File getPathFile() {
		File f = new File(path);
		if(f.isDirectory()) {
			return f;
		} else {
			return null;
		}
	}

	public File[] getChildren() {
		File f = new File(path);
		if(f.isDirectory()) {
			return f.listFiles();
		} else {
			return null;
		}
	}

	public File child(String filename) {
		return new File(path + File.separator + filename);
	}


	public void guardMaxFileCount(int count) {

		File path = this.getPathFile();
		if(path.isDirectory()) {

			File[] files = path.listFiles();
			if(files != null && files.length > count) {

				Arrays.sort(files, new Comparator<File>() {

					@Override
					public int compare(File file, File newFile) {
						if(file.lastModified() > newFile.lastModified()) {
							return 1;
						} else if(file.lastModified() == newFile.lastModified()) {
							return 0;
						} else {
							return -1;
						}
					}
				});

				for(int i = 0; i < files.length - count; i++) {

					File file = files[i];

					file.delete();

				}


			}

		}

	}

	public void guardMaxTotalLength(long maxLength) {

		File path = this.getPathFile();

		if(path.isDirectory()) {

			long total = 0;
			File[] files = path.listFiles();
			if(files != null) {
				for(File file : files) {
					total += file.length();
				}
			}

			log.info("total size: " + total + "byte");
			log.info("limit size: " + maxLength + "byte");

			if(total > maxLength) {

				Arrays.sort(files, new Comparator<File>() {

					@Override
					public int compare(File file, File newFile) {
						if(file.lastModified() > newFile.lastModified()) {
							return 1;
						} else if(file.lastModified() == newFile.lastModified()) {
							return 0;
						} else {
							return -1;
						}
					}
				});


				log.info("Stored data is bigger then limit, Guarding......");

				for(File file : files) {

					log.info(" Delete file:" + file.getName() + " size:" + file.length());

					total -= file.length();

					file.delete();

					if(total <= maxLength) {
						break;	
					}
				}

				log.info("New stored data total: " + total);

			}

		}

	}

	public long getSize() {
		return getSize(new File(path));
	}

	//public void deleteEmpty

	public static void deleteAllFilesOfDir(File path) {
		if (!path.exists())  
			return;  
		if (path.isFile()) {  
			path.delete();  
			return;  
		}  
		File[] files = path.listFiles();  
		for (int i = 0; i < files.length; i++) {  
			deleteAllFilesOfDir(files[i]);  
		}  
		path.delete();  
	}

	public static long getSize(File file) {
		
		long size = 0;
		
		//判断文件是否存在  
		if (file.exists()) {  
			//如果是目录则递归计算其内容的总大小，如果是文件则直接返回其大小  
			if (file.isDirectory()) {  
				//获取文件大小 
				File[] fl = file.listFiles();
				for (File f : fl)
					size += getSize(f);
			} else {  
				size += file.length();
			}  
		}
		
		return size;
	}  

}
